package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.payconfig.EntitySignatureRecord;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.SignatureListRequest;
import com.ocom.common.request.payconfig.SignatureUpdateRequest;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.vo.payconfig.SignatureRecordExxxVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoSignatureRecord;
import com.ocom.payconfig.service.SignatureRecordService;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SignatureRecordServiceImpl extends JoinServiceImpl<IDaoSignatureRecord, EntitySignatureRecord> implements SignatureRecordService {


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB ,isMaster = MasterSlaveEnum.MASTER)
    public boolean addSign(Long companyId, EntitySignatureRecord entitySignatureRecord) {
        return this.save(entitySignatureRecord);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB ,isMaster = MasterSlaveEnum.MASTER)
    public boolean revokedSign(Long companyId, SignatureUpdateRequest request) {
        LambdaUpdateWrapper<EntitySignatureRecord>   updateWrapper =new LambdaUpdateWrapper<>();
        updateWrapper.eq(EntitySignatureRecord::getComId,request.getComId());
        updateWrapper.eq(EntitySignatureRecord::getPId,request.getPId());
        if(request.getType()!=null){
            updateWrapper.eq(EntitySignatureRecord::getType,request.getType());
        }
        updateWrapper.eq(EntitySignatureRecord::getStatus, 1);
        updateWrapper.set(EntitySignatureRecord::getStatus, 0);
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB ,isMaster = MasterSlaveEnum.MASTER)
    public PageInfo<Map<String, Object>> getSignList(Long companyId, SignatureListRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        JoinLambdaWrapper<EntitySignatureRecord> queryWrapper =  new JoinLambdaWrapper<>(EntitySignatureRecord.class);
        queryWrapper.eq(EntitySignatureRecord::getComId,request.getComId());
        if(request.getPId()!=null){
            queryWrapper.eq(EntitySignatureRecord::getPId,request.getPId());
        }
        if(request.getStatus()!=null){
            queryWrapper.eq(EntitySignatureRecord::getStatus,request.getStatus());
        }
//        if(!CommonUtil.isNull(request.getPName())) {
//            queryWrapper.apply("personnel_info.p_name = {0}", request.getPName());
//        }



        queryWrapper.leftJoin(EntityPersonnelInfo.class, EntityPersonnelInfo::getPId, EntitySignatureRecord::getPId)
                .joinAnd(EntityPersonnelInfo::getComId,EntitySignatureRecord::getComId,0)
                .func(w -> {
//                    if(!CommonUtil.isNull(request.getPName())){
// queryWrapper.apply("personnel_info.p_name = {0}", request.getPName());
// w.eq(EntityPersonnelInfo::getName,request.getPName());
//                    }
                    w.eq(!CommonUtil.isNull(request.getPName()),EntityPersonnelInfo::getPName,request.getPName());
                })
                .selectAs((cb) -> {
                    cb.add(" personnel_info.p_name ","pName",false);
                    cb.add(" personnel_info.p_code ","pCode",false);
                }).end();
        List<SignatureRecordExxxVo> listFeeData =  this.joinList(queryWrapper, SignatureRecordExxxVo.class);
        return new PageInfo(listFeeData);

    }
}
